<?php
class UserModel extends CI_Model
{
    const collection = 'user';
    const sequence_id = 'user';
    
    // Cache
    const cache_enable = true;
    const cache_ttl = 86400;
    const prefix_cache_item = 'user.%s';
    const prefix_cache_list = 'user.list.%s';
    const prefix_cache_count = 'user.count.%s';
    
    public function __construct() 
    {
        parent::__construct();
    }
    
    public function initData($data)
    {
        // Check $data
        if(!is_array($data)) return null;

        // Define fields
        $fields = array('_id');

        $list = null;

        foreach($fields as $key) {
            if(isset($data[$key])) {
                $list[$key] = $data[$key];
            }
        }

        // Return
        return $list;
    }
    
    public function insert($data)
    {
        // Init data
        $data = $this->initData($data);

        // Check
        if(empty($data)) return false;

        // Get sequence id
        $CI = &get_instance();
        $CI->load->model('SequenceModel');
        $id = $CI->SequenceModel->getSequence(self::sequence_id);
        
        if(empty($id)) return false;

        // Set id
        $data['_id'] = $id;

        // Insert to DB
        $rs = $this->mongo_db->insert(self::collection, $data);

        // Check
        if($rs) {

            // Increment
            $CI->SequenceModel->increment(self::sequence_id);

            // Delete cache
        }

        return $rs;
    }
}
?>
